package com.smcdd.cdb.dbo.cdb_dbo.service.presenter;



import com.smcdd.cdb.dbo.cdb_dbo.app.App;
import com.smcdd.cdb.dbo.cdb_dbo.event.ReachEvent;
import com.smcdd.cdb.dbo.cdb_dbo.network.HttpObserver;
import com.smcdd.cdb.dbo.cdb_dbo.network.factory.RetrofitFactory;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.BaseResponseEntity;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.DayShop;
import com.smcdd.cdb.dbo.cdb_dbo.service.maneger.NetWorkCodeInfo;
import com.smcdd.cdb.dbo.cdb_dbo.service.view.IDetail;
import com.smcdd.cdb.dbo.cdb_dbo.ui.base.BaseFragmentActivity;
import com.smcdd.cdb.dbo.cdb_dbo.ui.base.BaseFragmentPresenter;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ObtainEncry;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ObtainVersion;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 作者：Administrator on 2017/7/6 12:52
 * 邮箱：zhanghuaiha@gmail.com
 */

public class DetailPresenter extends BaseFragmentPresenter<IDetail> {

    private String udid;
    private String eid;
    private String versionName;
    private List<DayShop> data;
    private List<DayShop> yet;
    private List<DayShop> not;

    public DetailPresenter(BaseFragmentActivity context) {
        super(context);
    }

    public void loadContacts(){
        yet = new ArrayList<>();
        not = new ArrayList<>();
    }


    public void getInfo(){
        udid = App.SP.getString("udid", "");
        eid = App.SP.getString("id", "");
        versionName = ObtainVersion.getVersionName(mContext);

        String time = System.currentTimeMillis() + "";
        String encry = ObtainEncry.getDay_shop("2", time, versionName, NetWorkCodeInfo.DAY_SHOP, udid, eid, "1000", "0");
        RetrofitFactory.getmInstanse().mApi.getDay_Shop("2",time, versionName, NetWorkCodeInfo.DAY_SHOP, udid,eid,"1000","0",encry)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpObserver<BaseResponseEntity<List<DayShop>>>() {
                    @Override
                    public void success(BaseResponseEntity<List<DayShop>> dayShopBaseResponseEntity) {
                        if (dayShopBaseResponseEntity.code == 200) {
                            data = dayShopBaseResponseEntity.data;
                            for (int i = 0; i < data.size(); i++) {
                                if (data.get(i).getIs_ok().equals("1")) {
                                    yet.add(data.get(i));
                                }else {
                                    not.add(data.get(i));
                                }
                            }
                            EventBus.getDefault().post(new ReachEvent(yet,not));
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        e.printStackTrace();
                    }
                });
    }
}
